var express = require('express')
var connection = require('../db/db_utill');

var router = express.Router()

//查询话题
router.get('/search', function (req, res) {
  const topic_keyword = req.query.keyword
  const word = '%' + topic_keyword + '%'
  connection.query(`SELECT * FROM topic_info where topic_state=0 and topic_title like ? or topic_content like ?`, [word, word], function (error, results, fields) {
    if (error) {
      throw error;
    } else if (results.length) {
      var string = JSON.stringify(results);
      var topic_info = JSON.parse(string);
      res.status(200).json({
        status: 200,
        //话题数据
        topic_info
      })
    } else {
      res.json({
        message: '没有这方面的话题！'
      })
    }
  });
})

module.exports = router;
